package com.zhiyou.servlet;

import com.zhiyou.model.Contract;
import com.zhiyou.model.Rent;
import com.zhiyou.service.RentService;
import com.zhiyou.service.impl.RentServiceImpl;
import com.zhiyou.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class RentServlet extends HttpServlet {
    private RentService rS=new RentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //接收参数
        String pageNoStr = req.getParameter("pageNo");
        System.out.println("pageNoStr="+pageNoStr);
        int pageNo = Integer.parseInt(pageNoStr);//第pageNo页
        System.out.println("pageNo="+pageNo);
        //接受模糊查询关键字
        String keyword1 = req.getParameter("keyword1");
        String keyword2 = req.getParameter("keyword2");

        //创建HashMap对象封装数据
        HashMap<String, String> keywordMap = new HashMap<>();
        keywordMap.put("keyword1",keyword1);
        keywordMap.put("keyword2",keyword2);

        int total = rS.getRentNum(keywordMap);//记录信息的数量
        //创建分页工具对象
        PageUtil pageUtil = new PageUtil(pageNo, total);
        List<Rent> rents =rS.getRent(pageNo,pageUtil.getPageSize(),keywordMap);
        req.getSession().setAttribute("rents",rents);
        req.getSession().setAttribute("keywordMap",keywordMap);
        req.getSession().setAttribute("pageUtil",pageUtil);
        resp.sendRedirect(req.getContextPath()+"/view/rent/list.jsp");
    }
}
